package com.piece.mall.service;

import com.piece.core.framework.enums.sql.SqlKey;
import com.piece.core.framework.support.bootstrap.Order;
import com.piece.core.framework.util.collection.MapUtil;
import com.piece.core.jpa.repository.BaseService;
import com.piece.mall.model.SmsMemberPrice;
import com.piece.mall.repository.SmsMemberPriceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class SmsMemberPriceService extends BaseService<SmsMemberPrice, String> {

    @Autowired
    private SmsMemberPriceRepository smsMemberPriceRepository;

    @PostConstruct
    public void initRepository() {
        setRepository(smsMemberPriceRepository);
    }

    /**
     * 根据sku主键查询会员价格
     */
    public List<SmsMemberPrice> findBySkuId(Long skuId) {
        return smsMemberPriceRepository.findByCondition(MapUtil.builder(new HashMap<String, Object>())
                .put("skuId", skuId).build(), Arrays.asList(new Order("memberLevelId", SqlKey.ASC)));
    }

    /**
     * 根据sku主键删除会员价格
     */
    public void deleteBySkuId(Long skuId) {
        smsMemberPriceRepository.deleteByProperties("skuId", skuId);
    }
}
